﻿using log4net.Repository.Hierarchy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Net7.WebApplication.Utility;
using ILoggerFactory = Microsoft.Extensions.Logging.ILoggerFactory;

namespace Net7.WebApplication.Controllers
{
    /// <summary>
    /// C:业务逻辑-控制器
    /// V：给用户展示界面--表现层--视图层
    /// M：实体模型，数据传输，模型
    /// </summary>
    public class A01_FirstController : Controller
    {
        private readonly ILogger<A01_FirstController> _logger;
        private readonly ILoggerFactory _ILoggerFactory;
        private readonly IConfiguration Configuration;

        private readonly ConnectionStringOptions _ConnectionStringOptions;
        private readonly ConnectionStringOptions _Options;

        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="logger"></param>
        public A01_FirstController(ILogger<A01_FirstController> logger,ILoggerFactory loggerFactory, IConfiguration configuration,
            IOptionsMonitor<ConnectionStringOptions> optionsMonitor, IOptions<ConnectionStringOptions> options)
        {
            _ConnectionStringOptions = optionsMonitor.CurrentValue;
            _Options = options.Value;

            _logger = logger;
            _logger.LogInformation($"_ILogger:{GetType().FullName} 被构造~~");


            _ILoggerFactory = loggerFactory;
            ILogger<A01_FirstController> _ILogger2 = _ILoggerFactory.CreateLogger<A01_FirstController>();
            _ILogger2.LogError($"_ILogger2: {GetType().FullName} 被构造~~");

            Configuration = configuration;
        }

        public IActionResult Index()
        {
            Console.WriteLine($"Id:{Configuration["Id"]}");
            Console.WriteLine($"Name:{Configuration["Name"]}");
            Console.WriteLine($"TeachInfo.Id:{Configuration["TeachInfo:Id"]}");
            Console.WriteLine($"TeachInfo.Name:{Configuration["TeachInfo:Name"]}");
            Console.WriteLine($"ConnectionStrings.WriteConnection:{Configuration["ConnectionStrings:WriteConnection"]}");
            Console.WriteLine($"ConnectionStrings.ReadConnectionList1:{Configuration["ConnectionStrings:ReadConnectionList:0"]}");
            Console.WriteLine($"ConnectionStrings.ReadConnectionList2:{Configuration["ConnectionStrings:ReadConnectionList:1"]}");
            Console.WriteLine($"ConnectionStrings.ReadConnectionList3:{Configuration["ConnectionStrings:ReadConnectionList:2"]}");



            //大量的业务逻辑处理
            //返回结果：给View展示
            base.ViewData["User1"] = "Kulala~~~~Kulala";
            ViewBag.User1 = "Kulala~";
            ViewData["User2"] = "闻";
            TempData["User3"] = "高强";
            //base.HttpContext.Session["User4"] = "非白"; 
            //最小化设计：通过扩展支持Session 
            HttpContext.Session.SetString("User4", "非白");
            object User5 = "角印";
            return View(User5);
        }
    }
}
